package com.zhulin.android.edoctor.fragment;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.zhulin.android.edoctor.R;
import com.zhulin.android.edoctor.db.DBDoctorDBHelp;
import com.zhulin.android.edoctor.jsonobject.JOHospitalModel;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.IInterface;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class FragmentRegister extends BaseFragment implements OnItemSelectedListener{

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//return super.onCreateView(inflater, container, savedInstanceState);
		View view=inflater.inflate(R.layout.fragement_register, container, false);
		initView(view);
		return view;
	}
	
	private EditText edtUserName,edtPhone,edtEmail,edtCustomerDoctor;
	private Spinner spinnerProvider,spinnerCity,spinnerDoctor,spinnerRoom;
	private void initView(View v){
		edtUserName=(EditText)v.findViewById(R.id.edtUserName);
		edtPhone=(EditText)v.findViewById(R.id.edtPhone);
		edtEmail=(EditText)v.findViewById(R.id.edtEmail);
		edtCustomerDoctor=(EditText)v.findViewById(R.id.edtCustomerDoctor);
		
		spinnerProvider=(Spinner)v.findViewById(R.id.spinnerProvider);
		spinnerCity=(Spinner)v.findViewById(R.id.spinnerCity);
		spinnerDoctor=(Spinner)v.findViewById(R.id.spinnerDoctor);
		spinnerRoom=(Spinner)v.findViewById(R.id.spinnerRoom);
		
		spinnerProvider.setOnItemSelectedListener(itemSelectedListenerProvince);
		spinnerCity.setOnItemSelectedListener(itemSelectedListenerCity);
		spinnerDoctor.setOnItemSelectedListener(itemSelectedListenerHospital);
		
		
		spinnerDoctor.setOnItemSelectedListener(this);
		
		loadData();
	}
	
	private void loadData(){
		DBDoctorDBHelp dbDoctorDBHelp=new DBDoctorDBHelp(getActivity(),DBDoctorDBHelp.DB_Name_Hospital);
		try {
			Dao<JOHospitalModel, Void> daoHospitalModel=dbDoctorDBHelp.getDaoHospital();
			QueryBuilder<JOHospitalModel, Void> queryBuilder= daoHospitalModel.queryBuilder();
			queryBuilder.groupBy("dProvinceName");
			List<JOHospitalModel> hospitalModels= daoHospitalModel.query(queryBuilder.prepare());
			
			List<String> objects=new ArrayList<String>();
			for (JOHospitalModel joHospitalModel : hospitalModels) {
				objects.add(joHospitalModel.getdProvinceName());
			}
			
			ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, objects);
					arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerProvider.setAdapter(arrayAdapter);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			dbDoctorDBHelp.close();
		}
	}
	
	private OnItemSelectedListener itemSelectedListenerProvince=new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			String textString=((TextView)view).getText().toString();
			DBDoctorDBHelp dbDoctorDBHelp=new DBDoctorDBHelp(getActivity(),DBDoctorDBHelp.DB_Name_Hospital);
			try {
				Dao<JOHospitalModel, Void> daoHospitalModel=dbDoctorDBHelp.getDaoHospital();
				QueryBuilder<JOHospitalModel, Void> queryBuilder= daoHospitalModel.queryBuilder();
				queryBuilder.groupBy("dCityName");//筛选掉重复的城市
				Where where=queryBuilder.where();
				where.eq("dProvinceName", textString);
				
				List<JOHospitalModel> hospitalModels= daoHospitalModel.query(queryBuilder.prepare());
				
				List<String> objects=new ArrayList<String>();
				for (JOHospitalModel joHospitalModel : hospitalModels) {
					objects.add(joHospitalModel.getdCityName());
				}
				ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, objects);
				arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spinnerCity.setAdapter(arrayAdapter);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			finally{
				dbDoctorDBHelp.close();
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			
		}
	};
	
	private OnItemSelectedListener itemSelectedListenerCity=new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			if(view==null){
				return;
			}
			String textString=((TextView)view).getText().toString();
			DBDoctorDBHelp dbDoctorDBHelp=new DBDoctorDBHelp(getActivity(),DBDoctorDBHelp.DB_Name_Hospital);
			try {
				Dao<JOHospitalModel, Void> daoHospitalModel=dbDoctorDBHelp.getDaoHospital();
				QueryBuilder<JOHospitalModel, Void> queryBuilder= daoHospitalModel.queryBuilder();
				Where where=queryBuilder.where();
				where.eq("dCityName", textString);
				
				List<JOHospitalModel> hospitalModels= daoHospitalModel.query(queryBuilder.prepare());
				
				List<String> objects=new ArrayList<String>();
				for (JOHospitalModel joHospitalModel : hospitalModels) {
					objects.add(joHospitalModel.getdHospitalName());
				}
				objects.add("其它");
				ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, objects);
				arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spinnerDoctor.setAdapter(arrayAdapter);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			finally{
				dbDoctorDBHelp.close();
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			
		}
	};
	
	private OnItemSelectedListener itemSelectedListenerHospital=new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			
		}
	};

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		if(position==(parent.getCount()-1)){
			edtCustomerDoctor.setVisibility(View.VISIBLE);
		}else{
			edtCustomerDoctor.setVisibility(View.GONE);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		
	}
}
